package com.china08.yxyapi.api.manage.stu.mystudy.tifenbao.exercise;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.es.ElasticYxyQuestionBankResource;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnowTrain;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnowTrainQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowTrainQuesRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowTrainRepository;
import com.china08.yxyapi.service.mg.impl.PlanExerciseServiceImpl;

/**
 * 获取答题记录详细信息
 *
 */
@RestController
public class TifenbaoKnowTrainSchController {

    @Autowired
    private YxyHuixuePlanKnowTrainRepository yxyHuixuePlanKnowTrainRepository;

    @Autowired
    private YxyHuixuePlanKnowTrainQuesRepository yxyHuixuePlanKnowTrainQuesRepository;

    @Autowired
    private PlanExerciseServiceImpl planExerciseServiceImpl;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/tifenbao/exercise/train/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        reqModel.check();

        YxyHuixuePlanKnowTrainQues trainQues = yxyHuixuePlanKnowTrainQuesRepository.findOne(reqModel.trainQuesId);
        Assert.notNull(trainQues, "答题记录不能为空");

        YxyHuixuePlanKnowTrain train = yxyHuixuePlanKnowTrainRepository.findOne(trainQues.getTrainId());
        Assert.notNull(train, "答题记录不能为空");

        ElasticYxyQuestionBankResource question = planExerciseServiceImpl.getExerciseById(trainQues.getQuesId());

        RespModel model = new RespModel();
        model.trainQuesId = trainQues.getId();
        model.questionBody = question.getQues();
        if (trainQues.getAnswerOfMy() != null) {
            model.answerOfMy = StringUtils.join(trainQues.getAnswerOfMy().toArray(), ",");
        } else {
            model.answerOfMy = "";
        }
        model.passed = trainQues.getPassed();
        model.time = trainQues.getUseTime();
        return model;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 答题记录id
        public String trainQuesId;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(trainQuesId, null), "答题记录id不能为空");
        }
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {
        // 答题记录id
        public String trainQuesId;

        // 试题body
        public Object questionBody;

        // 我的答案
        public String answerOfMy;

        // 是否正确 0、错误 1、正确
        public Integer passed;

        // 用时
        public String time;
    }
}
